package com.example.myapplication2;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.activity.ComponentActivity;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.myapplication2.adapter.MyFragmentPagerAdapter;
import com.example.myapplication2.fragment.CountFragment;
import com.example.myapplication2.fragment.HomeFragment;
import com.example.myapplication2.fragment.MeFragment;
import com.example.myapplication2.fragment.VideoFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    public MainActivity Self = this;

    public RelativeLayout titleBar;
    public TextView title;

    public RadioGroup radioGroup;
    public ViewPager viewPager;

    public void Init(){
        title = findViewById(R.id.title_index);
        titleBar = findViewById(R.id.title_bar);

        radioGroup = findViewById(R.id.btn_groud);
        viewPager = findViewById(R.id.viewPager);

        HomeFragment homeFragment = new HomeFragment();
        CountFragment countFragment = new CountFragment();
        VideoFragment videoFragment = new VideoFragment();
        MeFragment meFragment = new MeFragment();
        List<Fragment> alFragment = new ArrayList<Fragment>();
        alFragment.add(homeFragment);
        alFragment.add(countFragment);
        alFragment.add(videoFragment);
        alFragment.add(meFragment);
        viewPager.setOffscreenPageLimit(3);
        //ViewPager设置适配器
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
                alFragment));
        viewPager.setCurrentItem(0); //ViewPager显示第一个Fragment

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.home_btn){
                    title.setText("首页");
                    viewPager.setCurrentItem(0, true);
                } else if(checkedId == R.id.count_btn){
                    title.setText("统计");
                    viewPager.setCurrentItem(1, true);
                } else if(checkedId == R.id.video_btn){
                    title.setText("视频");
                    viewPager.setCurrentItem(2, true);
                } else if(checkedId == R.id.me_btn){
                    title.setText("我的");
                    viewPager.setCurrentItem(3, true);
                }
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.home_btn);
                        title.setText("首页");
                        break;
                    case 1:
                        radioGroup.check(R.id.count_btn);
                        title.setText("统计");
                        break;
                    case 2:
                        radioGroup.check(R.id.video_btn);
                        title.setText("视频");
                        break;
                    case 3:
                        radioGroup.check(R.id.me_btn);
                        title.setText("我的");
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable((ComponentActivity) this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);

            return insets;
        });

        Init();
    }
}